Quickly adding and deleting empty lines
Starting in normal mode, you can press O to insert a blank line before the current line, or o to insert one after. O and o ("open") also switch to insert mode so you can start typing. To add 10 empty lines below the cursor in normal mode, type 10o or to add them above the cursor type 10O. Here is an alternative method that allows you to easily insert or delete blank lines above or below the current line. Your cursor position is not changed, and you stay in normal mode. Put the following mappings in your vimrc: " Ctrl-j/k deletes blank line below/above, and Alt-j/k inserts. nnoremap m`:silent +g/\m^\s*$/d``:noh nnoremap m`:silent -g/\m^\s*$/d``:noh nnoremap :set pastem`o``:set nopaste nnoremap :set pastem`O``:set nopaste In normal mode, the mappings perform these operations without moving the cursor: *Ctrl-j deletes the line below the current line, if it is blank. *Ctrl-k deletes the line above the current line, if it is blank. *Alt-j inserts a blank line below the current line. *Alt-k inserts a blank line above the current line. Explanations *m` sets the context mark to the current cursor position. *`` jumps to the context mark to restore the cursor position. *The g/^\s*$/ commands search for a line matching ^ (begin line), then zero or more occurrences of whitespace, then $ (end line), that is, blank lines. *\m sets the 'magic' option so the pattern will work regardless of the current 'magic' option. *+g/pattern/d executes d (delete) on lines matching pattern in the range + (a single line after the current line). *:noh turns off any search highlighting. *:set paste sets Paste mode to temporarily switch off auto indenting so program comments won't be inserted (for example, in a cpp file, o on a //comment may insert // on the next line). See also *Remove unwanted empty lines to delete all blank lines. Alternative Following is from the original tip. Decide whether this is useful. If not, delete it. The cursor maintains its position in the window if possible. You should probably set scrolloffset to something other than 0 for optimal comfort. function! AddEmptyLineBelow() call append(line("."), "") endfunction function! AddEmptyLineAbove() let l:scrolloffsave = &scrolloff " Avoid jerky scrolling with ^E at top of window set scrolloff=0 call append(line(".") - 1, "") if winline() != winheight(0) silent normal! end let &scrolloff = l:scrolloffsave endfunction function! DelEmptyLineBelow() if line(".") line("$") return end let l:line = getline(line(".") + 1) if l:line =~ '^\s*$' let l:colsave = col(".") .+1d '' call cursor(line("."), l:colsave) end endfunction function! DelEmptyLineAbove() if line(".") 1 return end let l:line = getline(line(".") - 1) if l:line =~ '^\s*$' let l:colsave = col(".") .-1d silent normal! call cursor(line("."), l:colsave) end endfunction noremap :call DelEmptyLineBelow() noremap :call DelEmptyLineAbove() noremap :call AddEmptyLineBelow() noremap :call AddEmptyLineAbove() Following is a version that works in viemu and also doesn't conflict with preset keybindings hence this isn't strictly a VIM tip - delete it if you don't want it: " Ctrl-]/[ deletes line below/above, and Alt-]/[ inserts. nnoremap mzj:s/^\\s*\\n//i`z:noh nnoremap mzk:s/^\\s*\\n//i`z:noh nnoremap :mzo`z: nnoremap :mzO`z: Comments It does not work for me... it works like if I didn't configure anything :I am sorry this tip did not work for you, anonymous user. We would obviously like all our tips to work right away, to be correct, concise, and informative. However, without more information, we cannot fix any problems in this tip. :*What is "it"? Which script did you try using? :*What did you do to try to get "it" to "work"? I.e. how did you install and invoke the script? :*What didn't work about "it"? Did it do nothing at all? Did it do something incorrect? In other words, what were you expecting to happen, and what happened instead? :--Fritzophrenic 19:00, November 19, 2009 (UTC) Sorry, it is my first time... Here is my .vimrc source $VIMRUNTIME/macros/matchit.vim "script for matlab :filetype plugin on :set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab :set cindent :set smartindent :set autoindent "" from http://vim.wikia.com/wiki/Quickly_adding_and_deleting_empty_lines " Ctrl-j/k deletes blank line below/above, and Alt-j/k inserts. nnoremap m`:silent +g/\m^\s*$/d``:noh nnoremap m`:silent -g/\m^\s*$/d``:noh nnoremap :set pastem`o``:set nopaste nnoremap :set pastem`O``:set nopaste These nnoremap commands suggested before do not work for me... --IgorFobia 18:00, November 21, 2009 (UTC) ---- Hi Igor. "do not work for me" does not convey as much information as you might hope. What key did you press? What did you expect to happen? What did happen? I am not aware of a system where Ctrl-j or Ctrl-k or Alt-j or Alt-k do not work, but you might have such a system. You could try the following as a simple check: nnoremap m`:silent +g/\m^\s*$/d``:noh nnoremap m`:silent -g/\m^\s*$/d``:noh nnoremap :set pastem`o``:set nopaste nnoremap :set pastem`O``:set nopaste Pressing F4 or F5 should insert blank lines, and F2 or F3 should delete blank lines. These mappings definitely work. JohnBeckett 22:37, November 21, 2009 (UTC) ---- Thank you for your interest. After a better control, I realized that Ctrl-j and Ctrl-k work. But Alt-j and Alt-k do not work. Unfortunately, these four suggested mappings are not working for me. Using or not these mappings, acts like B acts like C acts like D acts like E I got this effect on the server (that is using the .vimrc posted before) and on my pc as well. --IgorFobia 00:34, November 22, 2009 (UTC) ---- I tried with your .vimrc as you posted, and to my surprise the mappings seemed to have almost no effect. However, the simple addition of "set nocompatible" at the top of your .vimrc fixed it on Ubuntu Karmic, gvim 7.2.293. I had been led to believe that this would get set automatically with the presence of a .vimrc of any kind, but apparently this is not the case. Regardless, 'nocompatible' allows access to most of Vim's powerful features. If there is any "essential" Vim option, this one is it. If this does not fix it for you, I would recommend moving this discussion to the vim_use mailing list. --Fritzophrenic 01:31, November 22, 2009 (UTC) ---- I solved the problem. I had some problem with the Alt key. Instead of nnoremap :set pastem`o``:set nopaste nnoremap :set pastem`O``:set nopaste I put in my .vimrc these two lines, nnoremap ^:set pastem`o``:set nopaste nnoremap ^[k :set pastem`O``:set nopaste where ^[j is created pressing Crtl-v and then Alt-j, as shown in http://vim.wikia.com/index.php?title=Get_Alt_key_to_work_in_terminal&oldid=23162 --[[User:IgorFobia|IgorFobia] 10:04, November 30, 2009 (UTC) :gvim recognizes A-j and terminal vim doesn't; be careful ^[j is the same as escape, j - you shouldn't be using escape in the normal mode too much though